package com.pimee.web.job;

import java.util.Date;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.pimee.common.util.DateUtil;
import com.pimee.common.util.shiro.ShiroUtils;
import com.pimee.common.util.web.Result;
import com.pimee.job.service.ISysJobService;
import com.pimee.model.SysJob;
import com.pimee.web.AbstractController;

import io.swagger.annotations.ApiOperation;

/**
 * 调度任务信息操作处理
 * 
 * @author Bruce Shaw 2020年2月5日 下午5:48:50
 *
 */
@Controller
@RequestMapping("/admin/sys/monitor/job")
public class SysJobController extends AbstractController {
	private String prefix = "admin/monitor/job";

	@Autowired
	private ISysJobService sysJobService;

	@ApiOperation("获取定时任务分页列表")
	@RequiresPermissions("monitor:job:list")
	@PostMapping("/list")
	@ResponseBody
	public Object list(@RequestParam Map<String, Object> params, ModelMap modelMap) {
		PageInfo<SysJob> list = sysJobService.pageList(params);
		return setSuccessModelMap(modelMap, list);
	}

	@ApiOperation("删除定时任务")
	@RequiresPermissions("monitor:job:remove")
	@PostMapping("/remove")
	@ResponseBody
	public Object remove(Long jobId) {
		sysJobService.deleteJobById(jobId);
		return Result.ok();
	}
	
	@ApiOperation("删除任意定时任务")
	@RequiresPermissions("monitor:job:remove")
	@PostMapping("/removeAny")
	@ResponseBody
	public Object remove(String ids) {
		sysJobService.deleteJobByIds(ids);
		return Result.ok();
	}

	@ApiOperation("进入定时任务详情页")
	@RequiresPermissions("monitor:job:detail")
	@GetMapping("/detail/{jobId}")
	public String detail(@PathVariable("jobId") Long jobId, ModelMap modelMap) {
		modelMap.put("name", "job");
		modelMap.put("job", sysJobService.selectByJobId(jobId));
		return prefix + "/detail";
	}

	@ApiOperation("任务调度状态修改")
	@RequiresPermissions("monitor:job:changeStatus")
	@PostMapping("/changeStatus")
	@ResponseBody
	public Object changeStatus(SysJob job) {
		SysJob newJob = sysJobService.selectByJobId(job.getJobId());
		newJob.setStatus(job.getStatus());
		newJob.setUpdateTime(DateUtil.now());
		sysJobService.changeStatus(newJob);
		return Result.ok();
	}

	@ApiOperation("任务调度立即执行一次")
	@RequiresPermissions("monitor:job:changeStatus")
	@PostMapping("/run")
	@ResponseBody
	public Object run(SysJob job) {
		sysJobService.run(job);
		return Result.ok();
	}

	@ApiOperation("进入新增调度页面")
	@GetMapping("/toAdd")
	public String add() {
		return prefix + "/add";
	}

	@ApiOperation("新增保存调度")
	@RequiresPermissions("monitor:job:add")
	@PostMapping("/add")
	@ResponseBody
	public Object addSave(@Validated SysJob param) {
		String loginName = ShiroUtils.getLoginName();
		param.setCreateBy(loginName);
		param.setUpdateBy(loginName);
		Date now = DateUtil.now();
		param.setCreateTime(now);
		param.setUpdateTime(now);
		sysJobService.insertJob(param);
		return Result.ok();
	}

	@ApiOperation("进入修改调度")
	@GetMapping("/toEdit/{jobId}")
	public String edit(@PathVariable("jobId") Long jobId, ModelMap mmap) {
		mmap.put("job", sysJobService.selectByJobId(jobId));
		return prefix + "/edit";
	}

	@ApiOperation("保存调度修改")
	@RequiresPermissions("monitor:job:edit")
	@PostMapping("/edit")
	@ResponseBody
	public Object editSave(@Validated SysJob job) {
		job.setUpdateTime(DateUtil.now());
		sysJobService.updateJob(job);
		return Result.ok();
	}

	@ApiOperation("校验cron表达式是否有效")
	@PostMapping("/checkCronExpressionIsValid")
	@ResponseBody
	public boolean checkCronExpressionIsValid(SysJob job) {
		return sysJobService.checkCronExpressionIsValid(job.getCronExpression());
	}
}
